<!doctype html>
<html lang="zh-CN"><head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script>
    <title>薛尧的博客</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="" />

    
    
    
    <link rel="stylesheet" href="../../../css/theme.min.css">

    
    
    
    
    <link rel="stylesheet" href="../../../css/custom.min.css">
    

    
</head>
<body>
        <div id="content" class="mx-auto"><header class="container mt-sm-5 mt-4 mb-4 mt-xs-1">
    <div class="row">
        
        <div class="col-sm-4 col-12 text-sm-right text-center pt-sm-4">
            <a href="../../../" class="text-decoration-none">
                <img id="home-image" class="rounded-circle"
                    
                        src="https://ueyao.github.io/image-hosting/blog/avatar.png"
                    
                />
            </a>
        </div>
        <div class="col-sm-8 col-12 text-sm-left text-center">
        
            <h2 class="m-0 mb-2 mt-4">
                <a href="../../../" class="text-decoration-none">
                    
                        薛尧
                    
                </a>
            </h2>
            <p class="text-muted mb-1">
                
                    Java Developer | Short Video Creator
                
            </p>
            <ul id="nav-links" class="list-inline mb-2">
                
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../about/" title="关于">关于</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../post/" title="文章">文章</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../categories/" title="分类">分类</a>
                    </li>
                
            </ul>
            <ul id="nav-social" class="list-inline">
                
                    <li class="list-inline-item mr-3">
                        <a href="http://github.com/flowstone" target="_blank">
                            <i class="fab fa-github fa-1x text-muted"></i>
                        </a>
                    </li>
                
                    <li class="list-inline-item mr-3">
                        <a href="mailto:xueyao.me#gmail.com" target="_blank">
                            <i class="fas fa-at fa-1x text-muted"></i>
                        </a>
                    </li>
                
            </ul>
        </div>
    </div>
    <hr />
</header>
<div class="container">
    <div class="pl-sm-2">
        <div class="mb-3">
            <h3 class="mb-0">Postman之token动态获取</h3>
            
            <small class="text-muted">发布于 2018-11-13</small>
        </div>

        <article>
            <p>目前项目涉及PC及APP端接口共用问题，后台接口给登陆后的用户设置了一个token，接口调用时请求头的参数值必须要动态生成，为了解决这个问题，查看Postman API文档，配置了可以方便后端开发者的Tests脚本，如果你需要，请按下面方式配置。</p>
<h3 id="用户登陆">用户登陆</h3>
<p>用户登陆页面的请求头参数为固定不变，如图所示</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2018/11/12/2018-11-12-5.36.35.png" alt=""></p>
<p>当填写正确的用户名和密码时，系统用返回如下图的数据，里面携带token的值,如图所示</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2018/11/12/2018-11-12-5.49.47.png" alt=""></p>
<p>在用户登陆测试接口页面，在点击Tests，在里面添加下面代码，如图所示：</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2018/11/12/2018-11-12-5.51.36.png" alt=""></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#a6e22e">pm</span>.<span style="color:#a6e22e">test</span>(<span style="color:#e6db74">&#34;Status code is 200&#34;</span>, <span style="color:#66d9ef">function</span> () {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">pm</span>.<span style="color:#a6e22e">response</span>.<span style="color:#a6e22e">to</span>.<span style="color:#a6e22e">have</span>.<span style="color:#a6e22e">status</span>(<span style="color:#ae81ff">200</span>);
</span></span><span style="display:flex;"><span>});
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">data</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">parse</span>(<span style="color:#a6e22e">responseBody</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//key值
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">key</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;要加密的Key&#39;</span>;
</span></span><span style="display:flex;"><span><span style="color:#75715e">//current-timestamp
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">currentTimestamp</span> <span style="color:#f92672">=</span>  <span style="color:#66d9ef">new</span> Date().<span style="color:#a6e22e">getTime</span>().<span style="color:#a6e22e">toString</span>();
</span></span><span style="display:flex;"><span><span style="color:#75715e">//nonce-str
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">nonceStr</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">getStr</span>(<span style="color:#ae81ff">32</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> <span style="color:#a6e22e">getStr</span>(<span style="color:#a6e22e">len</span>){
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">len</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">len</span> <span style="color:#f92672">||</span> <span style="color:#ae81ff">32</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">chars</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#39;</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">maxPos</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">chars</span>.<span style="color:#a6e22e">length</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">s</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;&#39;</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">let</span> <span style="color:#a6e22e">i</span> <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">i</span> <span style="color:#f92672">&lt;</span> <span style="color:#a6e22e">len</span>; <span style="color:#a6e22e">i</span><span style="color:#f92672">++</span>) {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">s</span> <span style="color:#f92672">+=</span> <span style="color:#a6e22e">chars</span>.<span style="color:#a6e22e">charAt</span>(Math.<span style="color:#a6e22e">floor</span>(Math.<span style="color:#a6e22e">random</span>() <span style="color:#f92672">*</span> <span style="color:#a6e22e">maxPos</span>));
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">s</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//token
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">token</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">data</span>.<span style="color:#a6e22e">data</span>.<span style="color:#a6e22e">token</span>;
</span></span><span style="display:flex;"><span><span style="color:#75715e">//拼接加密字符串
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">signStr</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">token</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">currentTimestamp</span>.<span style="color:#a6e22e">substring</span>(<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">10</span>) <span style="color:#f92672">+</span> <span style="color:#a6e22e">nonceStr</span>.<span style="color:#a6e22e">substring</span>(<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">16</span>) <span style="color:#f92672">+</span> <span style="color:#a6e22e">key</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">CryptoJS</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;crypto-js&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">var</span> <span style="color:#a6e22e">lpSign</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">CryptoJS</span>.<span style="color:#a6e22e">MD5</span>(<span style="color:#a6e22e">signStr</span>).<span style="color:#a6e22e">toString</span>();
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 设置环境变量token，供后面的接口引用
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">pm</span>.<span style="color:#a6e22e">environment</span>.<span style="color:#a6e22e">set</span>(<span style="color:#e6db74">&#34;token&#34;</span>, <span style="color:#a6e22e">data</span>.<span style="color:#a6e22e">data</span>.<span style="color:#a6e22e">token</span>);
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 设置环境变量current-timestamp，供后面的接口引用
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">pm</span>.<span style="color:#a6e22e">environment</span>.<span style="color:#a6e22e">set</span>(<span style="color:#e6db74">&#34;current-timestamp&#34;</span>, <span style="color:#a6e22e">currentTimestamp</span>);
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 设置环境变量current-timestamp，供后面的接口引用
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">pm</span>.<span style="color:#a6e22e">environment</span>.<span style="color:#a6e22e">set</span>(<span style="color:#e6db74">&#34;nonce-str&#34;</span>, <span style="color:#a6e22e">nonceStr</span>);
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 设置环境变量current-timestamp，供后面的接口引用
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">pm</span>.<span style="color:#a6e22e">environment</span>.<span style="color:#a6e22e">set</span>(<span style="color:#e6db74">&#34;lp-sign&#34;</span>, <span style="color:#a6e22e">lpSign</span>);
</span></span></code></pre></div><p>配置环境变量，因为每个接口都涉及请求头，所有我们用不用Postman中的环境变量，来实现，请求头动态更新</p>
<h2 id="步骤如下">步骤如下</h2>
<p>1、 打开设置
<img src="https://ueyao.github.io/image-hosting/blog/2018/11/12/2018-11-12-5.55.52.png" alt=""></p>
<p>2、 添加新环境
<img src="https://ueyao.github.io/image-hosting/blog/2018/11/12/2018-11-12-5.59.36.png" alt=""></p>
<p>3、 添加环境变量
<img src="https://ueyao.github.io/image-hosting/blog/2018/11/12/2018-11-12-7.14.38.png" alt=""></p>
<p>保存环境变量，在调用其它接口时，先选择环境，如下图所示</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2018/11/12/2018-11-12-7.18.57.png" alt=""></p>
<p>当Postman调用登陆接口时，会自动把缺少的环境变量值都添充完整，如下图所示</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2018/11/12/2018-11-12-7.24.30.png" alt=""></p>
<p>调用其它接口时，请求头引用环境变量，具体语法如下图所示</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2018/11/12/2018-11-12-8.05.02.png" alt=""></p>
<p>这样我们以后，调用其它接口，就不用每次都修改请求头数据，只要引用环境变量就完美解决问题。</p>
<p>注：老版本Postman有问题，本测试版本为6.5.2</p>

        </article>
    </div>

    <div id="disqus_thread"></div>
<script>
    window.disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "xie-yao-de-bo-ke" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

            </div>
        </div><footer class="text-center pb-1">
    <small class="text-muted">
        
        &copy; Copyright 2024
        
        |
        <a href="https://beian.miit.gov.cn/" target="_blank">苏ICP备14012079号</a>
        <br />
        由 <a href="https://gohugo.io/" target="_blank">Hugo</a> 强力驱动
        | 主题 <a href="https://github.com/austingebauer/devise" target="_blank">Devise</a>
        <br />

    </small>
</footer></body>
</html>
